var doAjax = require('./doAjax');

module.exports = function (type,name,params,data,success,msgbox_pointer,timeout) {
    if (msgbox_pointer) msgbox_pointer.setState({show: true, type: "loading", timeout: 0});

    var str = "";
    var isFirst = true;
    for (var i in params) {
        if (isFirst) {
            str += "?";
            isFirst = false;
        }
        else str += "&";
        str = str + i + "=" + params[i];
    }

    doAjax({
        type: type ? type : "POST",
        url: "http://aichong.leanapp.cn/" + name + str, //+ str ? str : "",//"/action" localhost:3000
        dataType: "JSON",
        data: data,
        timeout: timeout ? timeout : 4000,
        crossDomain: true,
        success: function (response) {
            if (response) {
                var result = JSON.parse(response);

                if (msgbox_pointer) {
                    if (result.msg)
                        msgbox_pointer.setState({timeout: 1200, type: "text", word: result.msg});
                    else msgbox_pointer.setState({show: false});
                }

                if (result.status == 200)
                    success(result);

                console.log(response);
            }
        },
        error: function (state,type) {
            if (msgbox_pointer)
                if (type == "timeout") {
                     msgbox_pointer.setState({timeout: 1200, type: "text", word: "请求超时"});
                } else {
                     msgbox_pointer.setState({timeout: 1200, type: "text", word: "请求失败"});
                }
        }
    });
}